
 import { put, takeEvery } from 'redux-saga/effects'
 import apiType, {
    GET_EMPLOYEE_LIST,
    SEARCH_EMPLOYEE_LIST,
    EDIT_EMPLOYEE_INFO,
    CREATE_EMPLOYEE_INFO,
    GET_EMPLOYEE_INVITE_TOKEN,
    GET_EMPLOYEE_H5_INFO,
    GET_ADMIN_EMPLOYEE_INFO,
    GET_ROLE_LIST,
    GET_ROLE_INFO,
    CREATE_ROLE,
    EDIT_ROLE,
    GET_PERMISSION_LIST,
    DEPART_EMPLOYEE,
    RE_ENROLL_EMPLOYEE,
    DELETE_EMPLOYEE,
    GET_SUPER_ADMIN,
    RESET_SUPER_ADMIN,
    GET_WEIXIN_CONFIG,
    GET_WEIXIN_OPEN_ID,
    JOIN_EMPLOYEE
 } from "@constants/ActionTypes";
 import {deleteAxios, getAxios, postAxios, putAxios} from "@utils/api";
 
 /**
  * 获取员工列表
  * @param search
  * @param status
  * @param pageNo
  * @param pageSize
  * @param callback
  * @returns {{callback: *, type: string, params: {pageNo: *, search: *, pageSize: *}}}
  */
 export const getEmployeeList = ({ search, status, pageNo, pageSize }, callback) => {
     const params = {
        search,
        status,
        pageNo,
        pageSize,
     }
     return {
         type: apiType(GET_EMPLOYEE_LIST).request,
         params,
         callback
     }
 }
 export const searchEmployee = ({ search }, callback) => {
     const params = {
        search,
        pageNo: 1,
        pageSize: 1,
     }
     return {
         type: apiType(SEARCH_EMPLOYEE_LIST).request,
         params,
         callback
     }
 }
//  创建员工
 export const createEmployee = (params,callback) => ({
    type: apiType(CREATE_EMPLOYEE_INFO).request,
    params,
    callback
})
//  修改员工信息
 export const editEmployeeInfo = (params,callback) => ({
    type: apiType(EDIT_EMPLOYEE_INFO).request,
    params,
    callback
})
// 获取员工邀请token
 export const getInviteToken = (params,callback) => ({
    type: apiType(GET_EMPLOYEE_INVITE_TOKEN).request,
    params,
    callback
})
// 获取用户信息
 export const getEmployeeInfo = (params,callback) => ({
    type: apiType(GET_ADMIN_EMPLOYEE_INFO).request,
    params,
    callback
})
// 根据token 获取用户信息（h5端）
 export const getInviteEmpInfo = (params,callback) => ({
    type: apiType(GET_EMPLOYEE_H5_INFO).request,
    params,
    callback
})
// 获取角色列表
 export const getRoleList = (params,callback) => ({
    type: apiType(GET_ROLE_LIST).request,
    params,
    callback
})
// 获取权限列表
 export const getPermissionList = (params,callback) => ({
    type: apiType(GET_PERMISSION_LIST).request,
    params,
    callback
})
// 获取角色详情
 export const getRoleInfo = (params,callback) => ({
    type: apiType(GET_ROLE_INFO).request,
    params,
    callback
})
// 编辑角色
export const editRole = (params, callback) => ({
    type: apiType(EDIT_ROLE).request,
    params,
    callback
})
// 创建角色
export const creatRole = (params, callback) => ({
    type: apiType(CREATE_ROLE).request,
    params,
    callback
})
// 员工离职
export const departEmployee = (params, callback) => ({
    type: apiType(DEPART_EMPLOYEE).request,
    params,
    callback
})
// 恢复任职
export const reEnrollEmployee = (params, callback) => ({
    type: apiType(RE_ENROLL_EMPLOYEE).request,
    params,
    callback
})
// 删除
export const deleteEmployee = (params, callback) => ({
    type: apiType(DELETE_EMPLOYEE).request,
    params,
    callback
})
// 获取超级管理员
export const getSuperAdmin = (params, callback) => ({
    type: apiType(GET_SUPER_ADMIN).request,
    params,
    callback
})
// 转让超级管理员
export const resetAdmin = (params, callback) => ({
    type: apiType(RESET_SUPER_ADMIN).request,
    params,
    callback
})
// 获取微信JS Config
export const getWeixinConfig = (params, callback) => ({
    type: apiType(GET_WEIXIN_CONFIG).request,
    params,
    callback
})
// 获取微信 open id
export const getUserOpenId = (params, callback) => ({
    type: apiType(GET_WEIXIN_OPEN_ID).request,
    params,
    callback
})
// 获取微信 open id
export const joinEmployee = (params, callback) => ({
    type: apiType(JOIN_EMPLOYEE).request,
    params,
    callback
})
 
 const sagas = {
     * [apiType(GET_EMPLOYEE_LIST).request]({params, callback}) {
         try {
             const response = yield getAxios('/admin/api/employee/v1/op/list', params)
             yield put({
                 type: apiType(GET_EMPLOYEE_LIST).success,
                 response,
             });
             callback && callback(response)
         } catch (e) {
             yield put({type: apiType(GET_EMPLOYEE_LIST).failure});
         }
     },
     * [apiType(SEARCH_EMPLOYEE_LIST).request]({params, callback}) {
         try {
             const response = yield getAxios('/admin/api/employee/v1/op/list', params)
             yield put({
                 type: apiType(SEARCH_EMPLOYEE_LIST).success,
                 response,
             });
             callback && callback(response)
         } catch (e) {
             yield put({type: apiType(SEARCH_EMPLOYEE_LIST).failure});
         }
     },
     * [apiType(CREATE_EMPLOYEE_INFO).request]({params, callback}) {
         try {
             const response = yield postAxios('/admin/api/employee/v1/invitation', params)
             yield put({
                 type: apiType(CREATE_EMPLOYEE_INFO).success,
                 response,
             });
             callback && callback()
         } catch (e) {
             yield put({type: apiType(CREATE_EMPLOYEE_INFO).failure});
         }
     },
     * [apiType(EDIT_EMPLOYEE_INFO).request]({params, callback}) {
         try {
             const response = yield putAxios('/admin/api/employee/v1/info/update', params)
             yield put({
                 type: apiType(EDIT_EMPLOYEE_INFO).success,
                 response,
             });
             callback && callback()
         } catch (e) {
             yield put({type: apiType(EDIT_EMPLOYEE_INFO).failure});
         }
     },
     * [apiType(GET_EMPLOYEE_INVITE_TOKEN).request]({params, callback}) {
         try {
             const response = yield getAxios('/admin/api/employee/v1/share/token', params)
             yield put({
                 type: apiType(GET_EMPLOYEE_INVITE_TOKEN).success,
                 response,
             });
             callback && callback()
         } catch (e) {
             yield put({type: apiType(GET_EMPLOYEE_INVITE_TOKEN).failure});
         }
     },
     * [apiType(GET_EMPLOYEE_H5_INFO).request]({params, callback}) {
         try {
             const response = yield getAxios('/admin/api/employee/v1/share/info', params)
             yield put({
                 type: apiType(GET_EMPLOYEE_H5_INFO).success,
                 response,
             });
             callback && callback(response)
         } catch (e) {
             yield put({type: apiType(GET_EMPLOYEE_H5_INFO).failure});
         }
     },
     * [apiType(GET_ADMIN_EMPLOYEE_INFO).request]({params, callback}) {
         try {
             const response = yield getAxios('/admin/api/employee/v1/info', params)
             yield put({
                 type: apiType(GET_ADMIN_EMPLOYEE_INFO).success,
                 response,
             });
             callback && callback(response)
         } catch (e) {
             yield put({type: apiType(GET_ADMIN_EMPLOYEE_INFO).failure});
         }
     },
     * [apiType(GET_ROLE_LIST).request]({params, callback}) {
         try {
             const response = yield getAxios('/admin/api/role/v1/list', params)
             yield put({
                 type: apiType(GET_ROLE_LIST).success,
                 response,
             });
             callback && callback(response)
         } catch (e) {
             yield put({type: apiType(GET_ROLE_LIST).failure});
         }
     },
     * [apiType(CREATE_ROLE).request]({params, callback}) {
         try {
             const response = yield postAxios('/admin/api/role/v1/create', params)
             yield put({
                 type: apiType(CREATE_ROLE).success,
                 response,
             });
             callback && callback(response)
         } catch (e) {
             yield put({type: apiType(CREATE_ROLE).failure});
         }
     },
     * [apiType(EDIT_ROLE).request]({params, callback}) {
         try {
             const response = yield putAxios('/admin/api/role/v1/update', params)
             yield put({
                 type: apiType(EDIT_ROLE).success,
                 response,
             });
             callback && callback(response)
         } catch (e) {
             yield put({type: apiType(EDIT_ROLE).failure});
         }
     },
     * [apiType(GET_ROLE_INFO).request]({params, callback}) {
         try {
             const response = yield getAxios(`/admin/api/role/v1/${params.id}`)
             yield put({
                 type: apiType(GET_ROLE_INFO).success,
                 response,
             });
             callback && callback(response)
         } catch (e) {
             yield put({type: apiType(GET_ROLE_INFO).failure});
         }
     },
     * [apiType(GET_PERMISSION_LIST).request]({params, callback}) {
         try {
            const response = yield getAxios(`/admin/api/role/v1/permission/list`,params)
             yield put({
                 type: apiType(GET_PERMISSION_LIST).success,
                 response,
             });
             callback && callback()
         } catch (e) {
             yield put({type: apiType(GET_PERMISSION_LIST).failure});
         }
     },
     * [apiType(DEPART_EMPLOYEE).request]({params, callback}) {
         try {
            const response = yield putAxios(`/admin/api/employee/v1/op/depart`,params)
             yield put({
                 type: apiType(DEPART_EMPLOYEE).success,
                 response,
             });
             callback && callback()
         } catch (e) {
             yield put({type: apiType(DEPART_EMPLOYEE).failure});
         }
     },
     * [apiType(RE_ENROLL_EMPLOYEE).request]({params, callback}) {
         try {
            const response = yield putAxios(`/admin/api/employee/v1/op/re-enroll`,params)
             yield put({
                 type: apiType(RE_ENROLL_EMPLOYEE).success,
                 response,
             });
             callback && callback()
         } catch (e) {
             yield put({type: apiType(RE_ENROLL_EMPLOYEE).failure});
         }
     },
     * [apiType(DELETE_EMPLOYEE).request]({params, callback}) {
         try {
            const response = yield putAxios(`/admin/api/employee/v1/op/delete`,params)
             yield put({
                 type: apiType(DELETE_EMPLOYEE).success,
                 response,
             });
             callback && callback()
         } catch (e) {
             yield put({type: apiType(DELETE_EMPLOYEE).failure});
         }
     },
     * [apiType(GET_SUPER_ADMIN).request]({params, callback}) {
         try {
            const response = yield getAxios(`/admin/api/employee/v1/sup-admin`,params)
             yield put({
                 type: apiType(GET_SUPER_ADMIN).success,
                 response,
             });
             callback && callback()
         } catch (e) {
             yield put({type: apiType(GET_SUPER_ADMIN).failure});
         }
     },
     * [apiType(RESET_SUPER_ADMIN).request]({params, callback}) {
         try {
            const response = yield putAxios(`/admin/api/employee/v1/sup-admin/reset`,params)
             yield put({
                 type: apiType(RESET_SUPER_ADMIN).success,
                 response,
             });
             callback && callback()
         } catch (e) {
             yield put({type: apiType(RESET_SUPER_ADMIN).failure});
         }
     },
     * [apiType(GET_WEIXIN_CONFIG).request]({params, callback}) {
         try {
            const response = yield getAxios(`/admin/api/vendor/v1/jsapi-params`,params)
             yield put({
                 type: apiType(GET_WEIXIN_CONFIG).success,
                 response,
             });
             callback && callback(response)
         } catch (e) {
             yield put({type: apiType(GET_WEIXIN_CONFIG).failure});
         }
     },
     * [apiType(GET_WEIXIN_OPEN_ID).request]({params, callback}) {
         try {
            const response = yield getAxios(`/admin/api/vendor/v1/mp/auth`,params)
             yield put({
                 type: apiType(GET_WEIXIN_OPEN_ID).success,
                 response,
             });
             callback && callback(response)
         } catch (e) {
             yield put({type: apiType(GET_WEIXIN_OPEN_ID).failure});
         }
     },
     * [apiType(JOIN_EMPLOYEE).request]({params, callback}) {
         try {
            const response = yield postAxios(`/admin/api/employee/v1/invitation/confirm`,params)
             yield put({
                 type: apiType(JOIN_EMPLOYEE).success,
                 response,
             });
             callback && callback(response)
         } catch (e) {
             yield put({type: apiType(JOIN_EMPLOYEE).failure});
         }
     },
 }
 
 
 export default Object.keys(sagas).map(item => {
     return function * s() {
         yield takeEvery(item, function *(args) {
             try {
                 yield sagas[item](args)
             } catch (e) {
                 console.log(e)
             }
         })
     }
 })